package com.operation.service.impl;

import com.operation.beans.Result;
import com.operation.beans.State;
import com.operation.beans.WorkOrderAllInfo;
import com.operation.dao.HandleInfoMapper;
import com.operation.dao.UserInfoMapper;
import com.operation.dao.WorkOrderMapper;
import com.operation.entity.HandleInfo;
import com.operation.entity.UserInfo;
import com.operation.entity.WorkOrder;
import com.operation.service.UserService;
import com.operation.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * Created by LCJ on 2017/10/24.
 */
@Service
public class UserServiceImpl extends State implements UserService{
    @Autowired
    WorkOrderMapper workOrderMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    HandleInfoMapper handleInfoMapper;

    @Override
    public Result register(UserInfo userInfo) {
        int i = userInfoMapper.insertSelective(userInfo);
        return ResultUtil.success(i,0);
    }

    @Override
    public Result add(UserInfo userInfo) {
        int i = userInfoMapper.insert(userInfo);
        return ResultUtil.success(i,0);
    }

    @Override
    public Result getUserInfo(UserInfo userInfo) {
        return ResultUtil.success(userInfoMapper.getUserInfo(userInfo),0);
    }

    @Override
    public int updateUserInfo(UserInfo userInfo) {

        return userInfoMapper.updateByUserAccountSelective(userInfo);
    }

    @Override
    public Result deleteUserInfo(Long userAccount) {
        userInfoMapper.deleteByUserAccount(userAccount);
        return null;
    }

    @Override
    public Result queryWorkOrderStatus(String jobNumber) {
        return ResultUtil.success(workOrderMapper.findWorkOrderStatus(jobNumber),0);
    }

    @Override
    public Result review(WorkOrder workOrder) {
        workOrder.setCommentTime(new Date());
        workOrder.setCompleteTime(new Date());
        workOrder.setIscomment(1);
        workOrder.setIsComplete(1);
        workOrderMapper.refreshComment(workOrder);
        /*HandleInfo handleInfo = new HandleInfo();
        handleInfo.setJobnumber(workOrder.getJobnumber());
        handleInfo.setStatus(3);
        handleInfoMapper.updateStatus(handleInfo);*/
        return ResultUtil.success(null,0);
    }

    @Override
    public Result confirmCompleteWorKOrder(WorkOrder workOrder) {
        workOrder.setIsComplete(1);
        workOrder.setCompleteTime(new Date());
        workOrderMapper.updateIsComplete(workOrder);
        return ResultUtil.success();
    }

    @Override
    public Result findHistoryWorkOrder(Integer userAccount) {

        return ResultUtil.success(workOrderMapper.findHistoryWorkOrder(userAccount),0);
    }

    @Override
    public int isExist(Long userAccount) {
        int code = 0;
        String msg = null;
        if (userInfoMapper.isExist(userAccount) == 0){
            code = 1;
            msg = "用户不存在";
        }else{
            code=2;
            msg = "用户已存在";
        }
        return code;
    }

    @Override
    public Result isOnline() {
        return ResultUtil.success(userInfoMapper.isOnline(),0);
    }

    @Override
    public int updateIsOnline(UserInfo userInfo) {
        return userInfoMapper.updateIsOnline(userInfo);
    }

    @Override
    public UserInfo isIt(UserInfo userInfo) {
        return userInfoMapper.isIt(userInfo);
    }

    @Override
    public WorkOrderAllInfo findRepairOrder(Long userAccount) {
        return workOrderMapper.findMyRepairWorkOrder(userAccount);
    }

    @Override
    public int complaint(String jobNumber) {
        return workOrderMapper.complaint(jobNumber);
    }

    @Override
    public int updateTypeByUserName(UserInfo userInfo) {
        return userInfoMapper.updateTypeByUserName(userInfo);
    }

    @Override
    public UserInfo findUserByType(Integer type) {
        return userInfoMapper.findUserByType(type);
    }

    @Override
    public UserInfo getUserTye(Long userAccount) {
        return userInfoMapper.getUserTye(userAccount);
    }

    @Override
    public List<UserInfo> getItPeople() {
        return userInfoMapper.getItPeople();
    }

    @Override
    public int updateItUserType(UserInfo userInfo) {
        return userInfoMapper.updateItUserType(userInfo);
    }
}
